package com.chaber.containers.beans;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.util.StringUtils;

@Entity
@Table(name = "product_categories")
@AttributeOverrides({
	@AttributeOverride(column = @Column(name = "prdcat_creation_date"), name = "creationDate"),
	@AttributeOverride(column = @Column(name = "prdcat_modification_date"), name = "modificationDate")
})
public class ProductCategoryData extends AbstractObjectData {

	private static final long serialVersionUID = 7246080456950728236L;

	@Id
	@Column(name = "prdcat_id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	
	@Column(name = "prdcat_code")
	@NotEmpty
	@Size(min = 1, max = 50)
	private String code;
	
	@Column(name = "prdcat_name")
	@NotEmpty
	@Size(min = 1, max = 50)
	private String name;
	
	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
	@JoinColumn(name = "prdcat_parentcat_id", referencedColumnName = "prdcat_id")
	private List<ProductCategoryData> childCategories = new ArrayList<>();
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "prdcat_parentcat_id",  referencedColumnName = "prdcat_id")
	private ProductCategoryData parentCategory;
	
	@OneToMany(fetch = FetchType.EAGER)
	@JoinColumn(name = "prd_prdcat_id", referencedColumnName = "prdcat_id")
	private List<ProductData> products;

	@Override
	public Integer getId() {
		return id;
	}
	
	public String getFullName() {
		if (StringUtils.isEmpty(this.code)) {
			return this.name;
		}
		
		return "[" + this.code + "] " + this.name;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<ProductData> getProducts() {
		return products;
	}

	public void setProducts(List<ProductData> products) {
		this.products = products;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<ProductCategoryData> getChildCategories() {
		return childCategories;
	}

	public void setChildCategories(List<ProductCategoryData> childCategories) {
		this.childCategories = childCategories;
	}

	public ProductCategoryData getParentCategory() {
		return parentCategory;
	}

	public void setParentCategory(ProductCategoryData parentCategory) {
		this.parentCategory = parentCategory;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("ProductCategoryData [id=");
		builder.append(id);
		builder.append(", code=");
		builder.append(code);
		builder.append(", name=");
		builder.append(name);
		builder.append(", childCategories=");
		builder.append(childCategories);
		builder.append(", parentCategory=");
		builder.append(parentCategory);
		builder.append(", products=");
		builder.append(products);
		builder.append("]");
		return builder.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductCategoryData other = (ProductCategoryData) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
